package pe.com.bn.sach.mantenimiento.controller;


import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import pe.com.bn.sach.seguridad.DatosSesion;
import pe.com.bn.sach.service.MantenimientoService;
import pe.com.bn.sach.service.SelectService;
import pe.com.bn.sach.mantenimiento.form.TablaForm;

//import pe.com.bn.sach.domain.Bnchf08Ubigeo;
import pe.com.bn.sach.domain.Bnchf06Tabla;
import pe.com.bn.sach.domain.Bnchf07Item;
import pe.com.bn.sach.domain.Bnchf07ItemId;


import java.text.ParseException;
import java.text.SimpleDateFormat;

public class TablaController extends MultiActionController {
	
    private String buscar;
    private String consultar;
    
    private MantenimientoService servicioMantenimiento;
    SelectService servicioSelect;
    private String commandName;
    private Class commandClass;
    /** Logger for this class and subclasses */    
    protected final Log logger = LogFactory.getLog(getClass());
    
    public TablaController()
    {
        ParameterMethodNameResolver resolver = new ParameterMethodNameResolver();
        resolver.setParamName("method");
        resolver.setDefaultMethodName("buscarTabla");
        setMethodNameResolver(resolver);
    }

    public ModelAndView busquedaTablaAjax(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		ModelAndView mav = new ModelAndView("mantenimiento/buscarTablaAjax");
		
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		TablaForm form = new TablaForm();
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		Bnchf06Tabla bnchf06Tabla= new Bnchf06Tabla();
		bnchf06Tabla.setF06DescTabla(form.getTxtDescTabla());
		if(form.getTxtCodTabla()!=null && !form.getTxtCodTabla().equals("")) 
			bnchf06Tabla.setF06IdTabla(new Long(form.getTxtCodTabla()));
		
		mav.addObject("lstTabla",servicioSelect.buscarTabla(bnchf06Tabla));
		mav.addObject("idTabla",form.getTxtCodTabla());
		mav.addObject("descTabla",form.getTxtDescTabla());
		
		return mav;
    }    
    
    public ModelAndView listarTablas(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/buscarTabla");
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;

		
		Bnchf06Tabla bnchf06Tabla= new Bnchf06Tabla();
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		bnchf06Tabla.setF06DescTabla("");
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("lstTabla",servicioSelect.listarTabla(bnchf06Tabla));
		return mav;
    }
    
    public ModelAndView nuevoTabla(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/registrarTabla");
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
	
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		
		return mav;
    }
   
    
    public ModelAndView guardarTabla(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		DatosSesion datosSesion = new DatosSesion();
		datosSesion = (DatosSesion)request.getSession().getAttribute("datosSesion");
		//String estado = form.getSelEstado();
		String descTabla =form.getTxtTabla();
		Bnchf06Tabla bnchf06Tabla= new Bnchf06Tabla();
		bnchf06Tabla.setF06DescTabla(descTabla.toUpperCase());
		bnchf06Tabla.setF06StdoTabla(new Long("1"));
		bnchf06Tabla.setF06IdUsuaCrea(datosSesion.getCodigoHost());
		servicioSelect.guardarTabla(bnchf06Tabla);
		return this.listarTablas(request,response);
    }
    
    public ModelAndView modificarTabla(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/actualizarTabla");
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		String estado = form.getSelEstado();
		String descTabla =form.getTxtTabla();
		String idTabla =form.getTxtIdTabla();
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("descTabla", descTabla);
		mav.addObject("idTabla", idTabla);
		mav.addObject("estado", estado);
		
		return mav;
    }
   
    
    public ModelAndView actualizarTabla(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		DatosSesion datosSesion = new DatosSesion();
		datosSesion = (DatosSesion)request.getSession().getAttribute("datosSesion");
		String estado = form.getSelEstado();
		String descTabla =form.getTxtTabla();
		String idTabla =form.getTxtIdTabla();
		Bnchf06Tabla bnchf06Tabla= new Bnchf06Tabla();
		bnchf06Tabla.setF06DescTabla(descTabla.toUpperCase());
		bnchf06Tabla.setF06IdTabla(new Long(idTabla));
		bnchf06Tabla.setF06StdoTabla(new Long(estado));
		bnchf06Tabla.setF06IdUsuaModi(datosSesion.getCodigoHost());
		servicioSelect.actualizarTabla(bnchf06Tabla);
		return this.listarTablas(request,response);
    }
    
    public ModelAndView consultarTabla(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/consultarTabla");
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		String idTabla = form.getTxtIdTabla();
		String estado = form.getSelEstado();
		String descTabla = form.getTxtTabla();
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("descTabla", descTabla);
		mav.addObject("idTabla", idTabla);
		mav.addObject("estado", estado);
		
		return mav;
    	
    }
    
    
    public ModelAndView busquedaItem(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		ModelAndView mav = new ModelAndView("mantenimiento/buscarItem");
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		Bnchf06Tabla bnchf06Tabla= new Bnchf06Tabla();
		Bnchf07Item bnchf07Item = new Bnchf07Item();
		Bnchf07ItemId bnchf07ItemId = new Bnchf07ItemId();
		bnchf06Tabla.setF06IdTabla(new Long(form.getTxtIdTabla()));
		if(form.getTxtCodItem()!=null && !form.getTxtCodItem().equals("")) 
			bnchf07ItemId.setF07IdItem(new Long(form.getTxtCodItem()));
		bnchf07ItemId.setBnchf06Tabla(bnchf06Tabla);
		bnchf07Item.setId(bnchf07ItemId);
		bnchf07Item.setF07DescExtItem(form.getTxtDescItem());
		bnchf07Item.setF07DescItem(form.getTxtDescItemCorta());
		bnchf07Item.setF07ValItem(form.getTxtDescItemValor());
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("lstItem",servicioSelect.buscarItem(bnchf07Item));
		mav.addObject("descCorta",form.getTxtDescItemCorta());
		mav.addObject("descTabla",form.getTxtTabla());
		mav.addObject("idTabla",form.getTxtIdTabla());
		mav.addObject("descLarga",form.getTxtDescItem());
		mav.addObject("valor",form.getTxtDescItemValor());
		mav.addObject("idItem",form.getTxtCodItem());
		
		return mav;
    }    
    
    public ModelAndView listarItem(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/buscarItem");
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;

		Bnchf06Tabla bnchf06Tabla= new Bnchf06Tabla();
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		String idTabla = form.getTxtIdTabla();
		String descTabla = form.getTxtTabla();
		Bnchf06Tabla tabla= new Bnchf06Tabla();
		Bnchf07Item itemB = new Bnchf07Item();
		Bnchf07ItemId itemBId = new Bnchf07ItemId();
		tabla.setF06IdTabla(new Long(idTabla));
		itemBId.setBnchf06Tabla(tabla);
		itemB.setId(itemBId);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("lstItem",servicioSelect.listarItem(itemB));
		mav.addObject("idTabla",idTabla);
		mav.addObject("descTabla",descTabla);
		return mav;
    }
    
    public ModelAndView nuevoItem(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/registrarItem");
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		String idTabla = form.getTxtIdTabla();
		String descTabla = form.getTxtTabla();
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("idTabla",idTabla);
		mav.addObject("descTabla",descTabla);
		
		return mav;
    }
   
    
    public ModelAndView guardarItem(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		DatosSesion datosSesion = new DatosSesion();
		datosSesion = (DatosSesion)request.getSession().getAttribute("datosSesion");
		Bnchf06Tabla bnchf06Tabla= new Bnchf06Tabla();
		Bnchf07Item bnchf07Item= new Bnchf07Item();
		Bnchf07ItemId bnchf07ItemId= new Bnchf07ItemId();
		bnchf06Tabla.setF06IdTabla(new Long(form.getTxtIdTabla()));
		bnchf07ItemId.setBnchf06Tabla(bnchf06Tabla);
		bnchf07Item.setId(bnchf07ItemId);
		bnchf07Item.setF07DescExtItem(form.getTxtItem());
		bnchf07Item.setF07DescItem(form.getTxtItemCorta());
		bnchf07Item.setF07ValItem(form.getTxtItemValor());
		bnchf07Item.setF07StdoItem(new Long(form.getSelEstado()));
		bnchf07Item.setF07IdUsuaCrea(datosSesion.getCodigoHost());
		servicioSelect.guardarItem(bnchf07Item);
		return this.listarItem(request,response);
    }
    
    
    public ModelAndView modificarItem(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/actualizarItem");
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		String descItem = form.getTxtItem();
		String descItemValor = form.getTxtItemValor();
		String descItemCorta = form.getTxtItemCorta();
		String idItem = form.getTxtIdItem();
		String estado = form.getSelEstado();
		String descTabla =form.getTxtTabla();
		String idTabla =form.getTxtIdTabla();
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("descTabla", descTabla);
		mav.addObject("descItem", descItem);
		mav.addObject("descItemValor", descItemValor);
		mav.addObject("descItemCorta", descItemCorta);
		mav.addObject("idTabla", idTabla);
		mav.addObject("idItem", idItem);
		mav.addObject("estado", estado);
		
		return mav;
    	
    	
    	
    }
   
    
    public ModelAndView actualizarItem(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		DatosSesion datosSesion = new DatosSesion();
		datosSesion = (DatosSesion)request.getSession().getAttribute("datosSesion");
		Bnchf06Tabla bnchf06Tabla= new Bnchf06Tabla();
		Bnchf07Item bnchf07Item= new Bnchf07Item();
		Bnchf07ItemId bnchf07ItemId= new Bnchf07ItemId();
		bnchf06Tabla.setF06IdTabla(new Long(form.getTxtIdTabla()));
		bnchf07ItemId.setBnchf06Tabla(bnchf06Tabla);
		bnchf07ItemId.setF07IdItem(new Long(form.getTxtIdItem()));
		bnchf07Item.setId(bnchf07ItemId);
		
		bnchf07Item.setF07DescExtItem(form.getTxtItem());
		bnchf07Item.setF07DescItem(form.getTxtItemCorta());
		bnchf07Item.setF07ValItem(form.getTxtItemValor());
		bnchf07Item.setF07StdoItem(new Long(form.getSelEstado()));
		bnchf07Item.setF07IdUsuaModi(datosSesion.getCodigoHost());
		servicioSelect.actualizarItem(bnchf07Item);
		return this.listarItem(request,response);
    }
    
    public ModelAndView consultarItem(HttpServletRequest request, HttpServletResponse response)
    throws Exception
    {
    	ModelAndView mav = new ModelAndView("mantenimiento/consultarItem");
		
		TablaForm form = new TablaForm();
		Object command = getCommandObject(request);
		form = (TablaForm)command;
		String descItem = form.getTxtItem();
		String descItemValor = form.getTxtItemValor();
		String descItemCorta = form.getTxtItemCorta();
		String idItem = form.getTxtIdItem();
		String estado = form.getSelEstado();
		String descTabla =form.getTxtTabla();
		String idTabla =form.getTxtIdTabla();
		Bnchf07ItemId itemid= new Bnchf07ItemId();
		Bnchf07Item item= new Bnchf07Item();
		itemid.getBnchf06Tabla().setF06IdTabla(new Long(40));
		item.setId(itemid);
		mav.addObject("lstEstado", servicioSelect.listItem(item));
		mav.addObject("descTabla", descTabla);
		mav.addObject("descItem", descItem);
		mav.addObject("descItemValor", descItemValor);
		mav.addObject("descItemCorta", descItemCorta);
		mav.addObject("idTabla", idTabla);
		mav.addObject("idItem", idItem);
		mav.addObject("estado", estado);
		
		return mav;
    }
    
   public Object getCommandObject(HttpServletRequest request)
    throws Exception
    {
    Object command = formBackingObject(request);
    bind(request, command);
    return command;
    }
    protected Object formBackingObject(HttpServletRequest request)
    throws Exception
    {
        return commandClass.newInstance();
    }

	public void setServicioMantenimiento(MantenimientoService servicioMantenimiento) {
		this.servicioMantenimiento = servicioMantenimiento;
	}


	public Class getCommandClass() {
		return commandClass;
	}


	public void setCommandClass(Class commandClass) {
		this.commandClass = commandClass;
	}


	public String getCommandName() {
		return commandName;
	}


	public void setCommandName(String commandName) {
		this.commandName = commandName;
	}

	public String copyString(String vari){
		if(vari == null || vari == "null" )
			vari="";
		return vari;
		
	}
	
	
	public int copyInt(String vari){
		if(vari == null || vari == "null" )
			vari="0";
		return Integer.parseInt(vari);
		
		
	}
	
	public java.util.Date copyDate(String vari) throws ParseException{
		SimpleDateFormat sdf= new SimpleDateFormat("dd/MM/yyyy");
		java.util.Date d = null;
		if(vari == null || vari == "null" )
			vari ="10-10-2000";
			d=sdf.parse(vari);
		return d;
		
	}


	public String getBuscar() {
		return buscar;
	}


	public void setBuscar(String buscar) {
		this.buscar = buscar;
	}

	public String getConsultar() {
		return consultar;
	}

	public void setConsultar(String consultar) {
		this.consultar = consultar;
	}


	
	/**
	 * @param servicioSelect El servicioSelect a establecer.
	 */
	public void setServicioSelect(SelectService servicioSelect) {
		this.servicioSelect = servicioSelect;
	}
}



